package com.lingkang.flychat.ui.activity.main;


import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.lingkang.flychat.config.UrlConfig;
import com.lingkang.flychat.websocket.WebsocketClient;
import com.orhanobut.hawk.Hawk;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class MainActivityWebSocket {

    public static void initWebSocket(Context context, Handler handler) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                WebsocketClient websocketClient = new WebsocketClient();
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(UrlConfig.ws + "?" + Hawk.get("token"))
                        .build();
                websocketClient.setHandler(handler);
                client.newWebSocket(request, websocketClient);
                client.dispatcher().executorService().shutdown();
            }
        }).start();

        /*Observable.create(new ObservableOnSubscribe<Map<String, Object>>() {
            @Override
            public void subscribe(ObservableEmitter<Map<String, Object>> emitter) throws Exception {
                WebsocketClient websocketClient = new WebsocketClient();
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(UrlConfig.ws + "?" + Hawk.get("token"))
                        .build();
                client.newWebSocket(request, websocketClient);
                client.dispatcher().executorService().shutdown();
            }
        })
                .subscribeOn(Schedulers.io())//切换到线程中
                .observeOn(AndroidSchedulers.mainThread()) // 回到主线程
                .subscribe(new Observer<Map<String, Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Map<String, Object> stringObjectMap) {
                        ++WebsocketClient.num;
                        Toast.makeText(context,"正在重连服务器..."+WebsocketClient.num,Toast.LENGTH_SHORT);
                        LogUtils.d("正在重连服务器..."+WebsocketClient.num);
                        if (stringObjectMap.get("c") != null) {
                                    initWebSocket(context);
                        }
                        LogUtils.d(stringObjectMap.get("123"));
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });*/
    }
}
